package com.baiytfp.hf.HFDataExchange.action.service;

import com.baiytfp.hf.HFDataExchange.action.dao.FuncDAOImpl;
import com.baiytfp.hf.HFDataExchange.action.dao.TreeDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.base.HFSysParams;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.model.table.Func;
import com.baiytfp.hf.HFDataExchange.common.model.table.Tree;
import com.baiytfp.hf.baiytfp.framework.base.BaseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FuncService extends BaseService {
	
	@Resource(name= HFSysParams.DB_TYPE + "FuncDAOImpl")
	private FuncDAOImpl funcDAOImpl;
	
	@Resource(name= HFSysParams.DB_TYPE + "TreeDAOImpl")
	private TreeDAOImpl treeDAOImpl;
	
	
	public List<Map> getTree(String treeType, String parentTreeCode) throws Exception{
		return funcDAOImpl.getTree(treeType, parentTreeCode);
	}

	public Func getFuncInfoByTreeCode(String treeCode) throws Exception {
		return funcDAOImpl.getFuncInfoByTreeCode(treeCode);
	}
	
	
	public void addFunc(Tree tree, Func func) throws Exception {
		tree.setNode_name(func.getFunc_name());
		treeDAOImpl.add(tree);
		func = funcDAOImpl.add(func);
	}

	public Map updateFunc(Map<String, String[]> parameterMap, int treeType, String treeCode) throws Exception {
		
		Tree treeVO = treeDAOImpl.getNodeInfo(treeCode, treeType + "",getConnection());
		Func func = funcDAOImpl.update(treeVO.getId(), parameterMap);
		
		BeanUtils.populate( treeVO,parameterMap);
		treeVO.setNode_name(func.getFunc_name());
		treeDAOImpl.update(treeVO);
		
		Map rs = new HashMap<>(16);
		rs.put("treeVO", treeVO);
		rs.put("funcVO", func);
		return rs;
	}
	
	public boolean  delFuncByTreeCode(String treeCode) throws Exception {
		boolean rs = funcDAOImpl.delByTreeCode(treeCode);
		if(rs) {
			rs = rs & treeDAOImpl.delByTreeCode(treeCode, TreeType.MANAGE_FUNC.getKey());
		}
		return rs;
		
	}
}
